tongfusoft.fieldsetMetadata = function() {
}
tongfusoft.fieldsetMetadata.prototype = {
	
	metadataListTable : function() {
		common.post('fieldsetMetadata/metadata/query', {
			"profileId" : $("#profileId").val()
		}, function(data) {
			$("#fieldsetData").html('');
			var fieldsetData='';
			if (data.state == 'success') {
				$.each(data.data, function(index, item) {					
					var fieldsetHtml = ' <tr>';
					fieldsetHtml = fieldsetHtml + '<td>';
					fieldsetHtml = fieldsetHtml +    '<div class="checkbox c-checkbox">';
					fieldsetHtml = fieldsetHtml +      '<label> <input type="checkbox" id="'+item.metadataId +'" name="choiceMetadata" value="'+item.metadataId+'"/> ';
					fieldsetHtml = fieldsetHtml +      '<span class="fa fa-check"></span>';
					fieldsetHtml = fieldsetHtml +      '</label>';
					fieldsetHtml = fieldsetHtml +    '</div>';
					fieldsetHtml = fieldsetHtml + '</td>';
					fieldsetHtml = fieldsetHtml + '<td>';
					fieldsetHtml = fieldsetHtml +    '<div class="checkbox c-checkbox">';
					fieldsetHtml = fieldsetHtml +      '<label> <input type="checkbox" name="isRequire" value="0"/> ';
					fieldsetHtml = fieldsetHtml +      '<span class="fa fa-check"></span>';
					fieldsetHtml = fieldsetHtml +      '</label>';
					fieldsetHtml = fieldsetHtml +    '</div>';
					fieldsetHtml = fieldsetHtml + '</td>';
					fieldsetHtml = fieldsetHtml + '<td>'+item.metadataName +'</td>';
					fieldsetHtml = fieldsetHtml + '<td>'+item.dictkeyName +'</td>';
					fieldsetHtml = fieldsetHtml + '<td> <input class="form-control" type="text" id="metaSort" name="metaSort" value="'+ item.metaSort +'"/> </td>';
					fieldsetHtml = fieldsetHtml + '</tr>';
						
					fieldsetData =fieldsetData+fieldsetHtml;
				});
			}
			$("#metadataListTable").html(fieldsetData);
		});
	},

	
	// 提交数据
	submitFieldsetMetadata : function() {

		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("fieldsetMetadataForm");
				initData['fieldsetId'] = $("#fieldsetId").val();
				var url = 'fieldsetMetadata/add';

				common.ajaxSubmitOnSuccess(initData, url, function(data) {

					if (data.state == 'success') {
						var opr = $("#opr").val();
						 if(opr === 'add'){
							 toastr["success"]('保存成功!');
							 
							 window.location.href = common.getContextPath('manager/inputProfile/fieldset/' + $("#profileId").val());
						 }else if(opr === 'edit'){
							 toastr["success"]('保存成功!');
						
							 window.location.href = common.getContextPath('manager/inputProfile/updateFieldsetHasMetadata/' + $("#fieldsetId").val());
							 
						 }
						
						
						$('#metadataTable').DataTable().ajax.reload();
						
					} else if (data.code == 'repeat_error') {

						toastr["error"]('参数编码不能重复!');
					} else{
						toastr["error"]('保存失败!');
					}

				}, $("#oper").val())
			}
		});
		$("#fieldsetMetadataForm").validate();

		$("#submitFieldsetMetadata").click(function() {
			    var bb = "";
				var temp = "";
				var metaSortTemp="";
				var metaSortVal ="";
				var isRequireTemp = "";
				var isRequireVal = "";
				var choiceMetadata = document.getElementsByName("choiceMetadata");
				var metaSort = document.getElementsByName("metaSort");
				var isRequire = document.getElementsByName("isRequire");
				for ( var i = 0; i < choiceMetadata.length; i++) {
					if (choiceMetadata[i].checked) {
					   temp = choiceMetadata[i].value;
					   bb = bb + "," +temp;
					   
					   for (var j = 0; j < isRequire.length; j++) {
						if(isRequire[i].checked){
							isRequireTemp = "1";
						}else{
							isRequireTemp = "0";
						}
					  }
					   isRequireVal = isRequireVal + "," + isRequireTemp;
					   
					   metaSortTemp = metaSort[i].value;
					   metaSortVal = metaSortVal + "," + metaSortTemp;
					   
					  
					}
				}
				$("#metadataIdList").val(bb);
				$("#metaSortList").val(metaSortVal);
				$("#isRequireList").val(isRequireVal);
				
			$("#fieldsetMetadataForm").submit();

		})
	},
	// 返回fieldSet页面
	returnFieldSet : function() {
		$("#returnBtn").click(function() {
			var opr = $("#opr").val();
			if(opr === 'add'){
				 window.location.href = common.getContextPath('manager/inputProfile/fieldset/' + $("#profileId").val());
			}else if(opr === 'edit'){
				 window.location.href = common.getContextPath('manager/inputProfile/updateFieldsetHasMetadata/' + $("#fieldsetId").val());
			}
		})
	},
	//全选
	metadataCheck : function(){
		var choiceMetadata = document.getElementsByName("choiceMetadata");
		var checkAll = document.getElementById("checkAll");
		$("#checkAll").click(function() {
			if(checkAll.checked == true) {
				for(var i = 0; i < choiceMetadata.length; i++){
					choiceMetadata[i].checked = true;
				}
			 } else {
				 for(var i = 0; i < choiceMetadata.length; i++){
					 choiceMetadata[i].checked = false;
					}
			 }
		});
	}
}

$(function() {
	var obj = new tongfusoft.fieldsetMetadata();
	obj.metadataListTable();
	obj.metadataCheck();
	obj.returnFieldSet();
	obj.submitFieldsetMetadata();
})